<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>博客类别管理页面</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

<script type="text/javascript">

	var url;

	function deleteBlogType(){
		var selectedRows=$("#dg").datagrid("getSelections");
		if(selectedRows.length==0){
			 $.messager.alert("系统提示","请选择要删除的数据！");
			 return;
		 }
		for(var i=0;i<selectedRows.length;i++){
			if(selectedRows[i].tableStatus == 1){
				$.messager.alert("系统提示","删除失败： "+selectedRows[i].tableName+"正在使用中！");
				return;
			}
			if(selectedRows[i].tableStatus == 2){
				$.messager.alert("系统提示","删除失败: "+selectedRows[i].tableName+"正在被预约中！");
				return;
			}	
		}
		 var strIds=[];
		 for(var i=0;i<selectedRows.length;i++){
			 strIds.push(selectedRows[i].id);
		 }
		 var ids=strIds.join(",");
		 $.messager.confirm("系统提示","您确定要删除这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
				if(r){
					$.post("${pageContext.request.contextPath}/delTable",{ids:ids},function(result){
						if(result.success){
							if(result.exist){
								 $.messager.alert("系统提示",result.exist);
							}else{
								 $.messager.alert("系统提示","数据已成功删除！");								
							}
							 $("#dg").datagrid("reload");
						}else{
							$.messager.alert("系统提示","数据删除失败！");
						}
					},"json");
				} 
	   });
	}
	
	function openBlogTypeAddDialog(){
		
		$("#dlg").dialog("open").dialog("setTitle","添加餐桌");
		resetValue();
		url="${pageContext.request.contextPath}/addTable";
	}
	
	function openBlogTypeModifyDialog(){
		var selectedRows=$("#dg").datagrid("getSelections");
		 if(selectedRows.length!=1){
			 $.messager.alert("系统提示","请选择一张要编辑的数据！");
			 return;
		 }
		 var row=selectedRows[0];
		 $("#dlg").dialog("open").dialog("setTitle","编辑餐信息");
		 $("#fm").form("load",row);
		 url="${pageContext.request.contextPath}/editTable?id="+row.id;
	 }
	
	function saveBlogType(){
		
		if($("#tableName").val().length>10){
			alert("名字不能超过10个字");
		}else{
			 $("#fm").form("submit",{
					url:url,
					onSubmit:function(){
						return $(this).form("validate");
					},
					success:function(result){
						var result=eval('('+result+')');
						if(result.success){
							$.messager.alert("系统提示","保存成功！");
							resetValue();
							$("#dlg").dialog("close");
							$("#dg").datagrid("reload");
						}else{
							$.messager.alert("系统提示","保存失败！");
							return;
						}
					}
				 }); 
		}
		
		
		/*  $("#fm").form("submit",{
			url:url,
			onSubmit:function(){
				return $(this).form("validate");
			},
			success:function(result){
				var result=eval('('+result+')');
				if(result.status==200){
					$.messager.alert("系统提示","保存成功！");
					resetValue();
					$("#dlg").dialog("close");
					$("#dg").datagrid("reload");
				}else{
					$.messager.alert("系统提示","保存失败！");
					return;
				}
			}
		 }); */
	 }
	
	
	
	
	function setBookTable(){
		var selectedRows=$("#dg").datagrid('getSelections');
		if(selectedRows.length==0){
			$.messager.alert("系统提示","请选择要预定的桌子！");
			return;

		}
		var isBook = true;
		for(var i=0;i<selectedRows.length;i++){
			if(selectedRows[i].tableStatus == 1){
				$.messager.alert("系统提示",selectedRows[i].tableName+"正在使用中！");
				return;
			}
			if(selectedRows[i].tableStatus == 0){
				isBook = false;
			}	
		}

		if(isBook){
			$.messager.alert("系统提示","所选餐桌已被预约！");
			return;
		}
		var strIds=[];
		for(var i=0;i<selectedRows.length;i++){
			strIds.push(selectedRows[i].id);
		}
		var id=strIds.join(",");
		$.messager.confirm("系统提示","您确认要预定这<font color=red>"+selectedRows.length+"</font>张桌子吗？",function(r){
			if(r){
				$.post("${pageContext.request.contextPath}/bookTable",{id:id,tableStatus:2},function(result){
					if(result.success){
						$.messager.alert("系统提示","预定成功！");
						$("#dg").datagrid("reload");
					}else{
						$.messager.alert("系统提示","预定成功！");
					}
				},"json");
			}
		});
	}
	
	function removeBookTable(){
		var selectedRows=$("#dg").datagrid('getSelections');
		if(selectedRows.length==0){
			$.messager.alert("系统提示","请选择要取消预定的桌子！");
			return;
		}
		var isBook = true;
		for(var i=0;i<selectedRows.length;i++){
			if(selectedRows[i].tableStatus == 1){
				$.messager.alert("系统提示",selectedRows[i].tableName+"正在使用中！");
				return;
			}
			if(selectedRows[i].tableStatus == 2){
				isBook = false;
			}	
		}
		if(isBook){
			$.messager.alert("系统提示","所选餐桌已空闲中！");
			return;
		}
		var strIds=[];
		for(var i=0;i<selectedRows.length;i++){
			strIds.push(selectedRows[i].id);
		}
		var id=strIds.join(",");
		$.messager.confirm("系统提示","您确认要取消预定这<font color=red>"+selectedRows.length+"</font>张桌子吗？",function(r){
			if(r){
				$.post("${pageContext.request.contextPath}/bookTable",{id:id,tableStatus:0},function(result){
					if(result.success){
						
						$.messager.alert("系统提示","取消预定成功！");
						$("#dg").datagrid("reload");
					}else{
						$.messager.alert("系统提示","取消预定成功！");
					}
				},"json");
			}
		});
	}
	
	
	
	
	
	
	
	 
	function resetValue(){
		 $("#tableName").val("");
		 $("#tableStatus").val("");
	 }
	
	 function closeBlogTypeDialog(){
		 $("#dlg").dialog("close");
		 resetValue();
	 }
	 
	 function formatState(val,row)
	 {
		 if(val==0){
			 return "<font color='green'>空闲</font>";
		 }else if(val==2){
			 return "被预约";
		 }else{
			 return "<font color='red'>使用中</font>";
		 }
		 
		 
		 
		 
		 
	 }
	
</script>
</head>
<body style="margin: 1px" >
<table id="dg" title="餐桌管理" class="easyui-datagrid"
   fitColumns="true" pagination="true" rownumbers="true"
   url="${pageContext.request.contextPath}/dinnerTables" fit="true" toolbar="#tb">
   <thead>
   	<tr>
   		<th field="cb" checkbox="true" align="center"></th>
   		<th field="id" width="20" align="center">编号</th>
   		<th field="tableName" width="100" align="center">餐桌名字</th>
   		<th field="tableStatus" width="100" align="center" formatter="formatState">餐桌使用状态</th>
   	</tr>
   </thead>
 </table>
 <div id="tb">
 	<div>
 	    <a href="javascript:openBlogTypeAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加桌子</a>
 	    <a href="javascript: openBlogTypeModifyDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">编辑桌子</a>
 		<a href="javascript:setBookTable()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">预定桌子</a>
 		<a href="javascript:removeBookTable()" class="easyui-linkbutton" iconCls="icon-no" plain="true">取消预定</a>
 		<a href="javascript:deleteBlogType()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除桌子</a>
 	</div>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
 </div>
 
 
 <div id="dlg" class="easyui-dialog" style="width:500px;height:180px;padding: 10px 20px"
   closed="true" buttons="#dlg-buttons">
   
   <form id="fm" method="post">
   	<table cellspacing="8px">
   		<tr>
   			<td>餐桌名称：</td>
   			<td><input type="text" id="tableName" name="tableName" class="easyui-validatebox" required="true"/></td>
   		</tr>
   		<!-- <tr>
   			<td>餐桌使用状态：</td>
   			<td><input type="text" id="status" name="status" class="easyui-validatebox" required="true"/></td>
   		</tr> -->
   		
   		
   	</table>
   </form>
 </div>
 
 <div id="dlg-buttons">
 	<a href="javascript:saveBlogType()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
 	<a href="javascript:closeBlogTypeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
 </div>
</body>
</html>